import json
import jieba
import numpy as np
import PIL.Image as image
from matplotlib import pyplot as plt
from wordcloud import WordCloud

plt.rcParams['font.sans-serif'] = ['SimHei']  # 设置字体为黑体
# 读取 JSON 文件
with open('backend\output.json', encoding='utf-8') as f:
    data = json.load(f)
    print('JSON 数据加载成功，内容如下：')
    print(data)

# 提取导演信息
directors = []
for item in data:
    directors.append(item['director'])
director_text = ' '.join(directors)
director_word_list = jieba.cut(director_text)
director_new_list = ' '.join(director_word_list)

# 提取电影名字信息
movie_names = []
for item in data:
    movie_names.append(item['movie_name'])
movie_text = ' '.join(movie_names)
movie_word_list = jieba.cut(movie_text)
movie_new_list = ' '.join(movie_word_list)

# 提取电影类型信息
genres = []
for item in data:
    genres.append(item['genre'])
genre_text = ''.join(genres)
genre_word_list = jieba.cut(genre_text)
genre_new_list = ''.join(genre_word_list)


# 提取电影国家信息
countries = []
for item in data:
    # 拆分每个电影的国家信息
    country_list = item['country'].split()
    countries.extend(country_list)
# 去重
unique_countries = list(set(countries))
# 合并成字符串，生成词云文本
country_text = ' '.join(unique_countries)
country_word_list = jieba.cut(country_text)
country_new_list = ''.join(country_word_list)


# 生成导演名字词云
img_mask1 = np.array(image.open('backend//data//timg.jpg'))
director_wordcloud = WordCloud(
    background_color='white',
    width=1000,
    height=700,
    mask=img_mask1,
    font_path='data//simfang.ttf',
).generate(director_new_list)


# 生成电影名字词云
img_mask2 = np.array(image.open('backend//data//timg4.jpg'))
movie_wordcloud = WordCloud(
    background_color='white', mask=img_mask2, font_path='data//simfang.ttf'
).generate(movie_new_list)


# 生成电影类型词云
img_mask3 = np.array(image.open('backend//data//timg4.jpg'))
genre_wordcloud = WordCloud(
    background_color='white', mask=img_mask3, font_path='data//simfang.ttf'
).generate(genre_new_list)

# 生成电影国家词云``
img_mask4 = np.array(image.open('backend//data//timg.jpg'))
country_wordcloud = WordCloud(
    background_color='white', mask=img_mask4, font_path='data//simfang.ttf'
).generate(country_new_list)


# 显示导演名字词云
plt.imshow(director_wordcloud)
plt.axis('off')
plt.title('导演名字词云')
plt.show()

# 显示电影名字词云
plt.imshow(movie_wordcloud)
plt.axis('off')
plt.title('电影名字词云')
plt.show()

# 显示电影类型词云
plt.imshow(genre_wordcloud)
plt.axis('off')
plt.title('电影类型词云')
plt.show()

# 显示电影年份词云
plt.imshow(country_wordcloud)
plt.axis('off')
plt.title('电影国家词云')
plt.show()
